package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.TbTaskDetailLogsVO;
import com.ruoyi.system.mapper.TbDeviceInfoMapper;
import com.ruoyi.system.mapper.TbDeviceMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TbTaskDetailLogsMapper;
import com.ruoyi.system.domain.TbTaskDetailLogs;
import com.ruoyi.system.service.ITbTaskDetailLogsService;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

/**
 * 任务执行日志Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-12
 */
@Service
public class TbTaskDetailLogsServiceImpl implements ITbTaskDetailLogsService {
    @Autowired
    private TbTaskDetailLogsMapper tbTaskDetailLogsMapper;


    @Resource
    private TbDeviceMapper tbDeviceMapper;

    @Resource
    private TbDeviceInfoMapper tbDeviceInfoMapper;

    /**
     * 查询任务执行日志
     *
     * @param id 任务执行日志主键
     * @return 任务执行日志
     */
    @Override
    public TbTaskDetailLogs selectTbTaskDetailLogsById(Long id) {
        return tbTaskDetailLogsMapper.selectTbTaskDetailLogsById(id);
    }

    /**
     * 查询任务执行日志列表
     *
     * @param tbTaskDetailLogs 任务执行日志
     * @return 任务执行日志
     */
    @Override
    public List<TbTaskDetailLogs> selectTbTaskDetailLogsList(TbTaskDetailLogs tbTaskDetailLogs) {
        return tbTaskDetailLogsMapper.selectTbTaskDetailLogsList(tbTaskDetailLogs);
    }

    /**
     * 新增任务执行日志
     *
     * @param tbTaskDetailLogs 任务执行日志
     * @return 结果
     */
    @Override
    public int insertTbTaskDetailLogs(TbTaskDetailLogs tbTaskDetailLogs) {
        tbTaskDetailLogs.setCreateTime(DateUtils.getNowDate());
        return tbTaskDetailLogsMapper.insertTbTaskDetailLogs(tbTaskDetailLogs);
    }

    /**
     * 修改任务执行日志
     *
     * @param tbTaskDetailLogs 任务执行日志
     * @return 结果
     */
    @Override
    public int updateTbTaskDetailLogs(TbTaskDetailLogs tbTaskDetailLogs) {
        tbTaskDetailLogs.setUpdateTime(DateUtils.getNowDate());
        return tbTaskDetailLogsMapper.updateTbTaskDetailLogs(tbTaskDetailLogs);
    }

    /**
     * 批量删除任务执行日志
     *
     * @param ids 需要删除的任务执行日志主键
     * @return 结果
     */
    @Override
    public int deleteTbTaskDetailLogsByIds(Long[] ids) {
        return tbTaskDetailLogsMapper.deleteTbTaskDetailLogsByIds(ids);
    }

    /**
     * 删除任务执行日志信息
     *
     * @param id 任务执行日志主键
     * @return 结果
     */
    @Override
    public int deleteTbTaskDetailLogsById(Long id) {
        return tbTaskDetailLogsMapper.deleteTbTaskDetailLogsById(id);
    }

    @Override
    public List<TbTaskDetailLogsVO> selectTbTaskDetailLogsLis(Long taskId, String deviceSn, Integer success) {
        TbTaskDetailLogsVO detailLogsVO = new TbTaskDetailLogsVO();
        List<TbTaskDetailLogsVO> taskDetailLogsVOS = new ArrayList<>();


        List<TbTaskDetailLogs> list = tbTaskDetailLogsMapper.selectTbTaskDetailLogsLis(taskId, deviceSn, success);
        if (list != null && list.size() > 0) {
            for (TbTaskDetailLogs tbTaskDetailLogs : list) {
                BeanUtils.copyProperties(tbTaskDetailLogs, detailLogsVO);
                if (tbTaskDetailLogs.getPushPackageType() == 1) {
                    detailLogsVO.setAppOrPackageName(tbTaskDetailLogs.getAppName());
                }
                if (tbTaskDetailLogs.getPushPackageType() == 2) {
                    detailLogsVO.setAppOrPackageName(tbTaskDetailLogs.getPackageName());
                }
                taskDetailLogsVOS.add(detailLogsVO);
            }
        }
        return taskDetailLogsVOS;
    }
}
